package handler

import (
	"github.com/gin-gonic/gin"
	"zy11/pkg/jwt"
	"zy11/repository"
	"zy11/request"
	"zy11/response"
)

const JWT_USER_ID = "2210A"

func Login(c *gin.Context) {
	var req request.LoginUser
	err := c.ShouldBind(&req)
	if err != nil {
		response.ResponseError(c, "错误", err.Error())
		return
	}
	loginUser, err := repository.LoginUser(req.Mobile)
	if loginUser.Id == 0 {
		response.ResponseError(c, "没找到", err)
		return
	}
	if loginUser.Password != req.Password {
		response.ResponseError(c, "密码错误", nil)
		return
	}
	claims := jwt.CustomClaims{ID: loginUser.Id}
	token, err := jwt.NewJWT(JWT_USER_ID).CreateToken(claims)
	if err != nil {
		response.ResponseError(c, "token失败", err)
		return
	}
	response.ResponseSuccess(c, "成功了", token)
}
